package eu.vranckaert.workRegistrationWidget.widgetProvider;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import eu.vranckaert.workRegistrationWidget.R;
import eu.vranckaert.workRegistrationWidget.activities.RegisterWorkActivity;
import eu.vranckaert.workRegistrationWidget.activities.ResultsActivity;
import eu.vranckaert.workRegistrationWidget.dao.TimeRegDao;
import eu.vranckaert.workRegistrationWidget.dao.impl.TimeRegDaoImpl;
import eu.vranckaert.workRegistrationWidget.domain.TimeRegistration;
import eu.vranckaert.workRegistrationWidget.service.WidgetLayoutBuilderService;
import eu.vranckaert.workRegistrationWidget.service.impl.WidgetLayoutBuilderServiceImpl;
import eu.vranckaert.workRegistrationWidget.util.WidgetUpdater;

/**
 * @author Dirk Vranckaert
 * Date: 21-apr-2010
 * Time: 22:12:16
 */
public class WorkRegistrationWidgetProvider extends AppWidgetProvider {
    private static final String LOG_TAG = WorkRegistrationWidgetProvider.class.getName();

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(LOG_TAG, "UPDATE");
        final int N = appWidgetIds.length;
        Log.d(LOG_TAG, "Number of widget = " + N);

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {

            int appWidgetId = appWidgetIds[i];
            AppWidgetProviderInfo widgetProviderInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);
            Log.d(LOG_TAG, "STARTING FOR WIDGET ID: " + appWidgetId);
            Log.d(LOG_TAG, "PROVIDER: " + widgetProviderInfo.provider.toString());

            TimeRegDao timeRegDao = new TimeRegDaoImpl();
            WidgetLayoutBuilderService widgetLayoutBuilderService = new WidgetLayoutBuilderServiceImpl();
            TimeRegistration registration = timeRegDao.findLatestRegistration(context);
            WidgetUpdater.updateWidget(context, registration, widgetLayoutBuilderService);
        }
	}

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(LOG_TAG, "RECEIVE");
        super.onReceive(context, intent);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Log.d(LOG_TAG, "DELETED");
        super.onDeleted(context, appWidgetIds);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onEnabled(Context context) {
        Log.d(LOG_TAG, "ENABLED");
        super.onEnabled(context);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void onDisabled(Context context) {
        Log.d(LOG_TAG, "DISABLED");
        super.onDisabled(context);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
